//
//  MyInfomationViewController.m
//  didacar
//
//  Created by 度周末网络-王腾 on 15/11/27.
//  Copyright © 2015年 dzmmac. All rights reserved.
//

#import "MyInfomationViewController.h"
#import "MyInfomationCell.h"
#import "MyCharteredBusViewController.h"
#import "UserInfoManager.h"
#import "SettingViewController.h"
#import "UIButton+Dzm.h"
#import "FeedBackViewController.h"
#import "CommonAddressViewController.h"
#import "MyCustomViewController.h"
#import "H5BaseViewController.h"
#import "TravelViewController.h"
#import "TicketViewController.h"
#import "MyOrderViewController.h"
@interface MyInfomationViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (strong, nonatomic) NSArray *dataSourceNames;
@property (strong, nonatomic) NSArray *dataSourceImages;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (weak, nonatomic) IBOutlet UIView *headerView;
@property (weak, nonatomic) IBOutlet UIImageView *headerImage;

@property (weak, nonatomic) IBOutlet UIView *footerView;
@property (weak, nonatomic) IBOutlet UILabel *IDLabel;

@property (weak, nonatomic) IBOutlet UIView *loginFooterView;
@property (weak, nonatomic) IBOutlet UIButton *loginButton;


- (IBAction)loginAction:(id)sender;

- (IBAction)myOrderAction:(id)sender;

- (IBAction)myJourneyAction:(id)sender;

@end

@implementation MyInfomationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 初始化列表数据源
    [self initTableViewDataSource];
    [CommoneTools changeConstraintView:self.headerImage constraintHeight:GTFixHeightFloat(137)];
    self.headerView.frame = CGRectMake(0, 0, [UIScreen width], GTFixHeightFloat(137)+40);
    
    
    // 注册通知
    [self addLoginStatusNotificenter];
    
    [self setUpHeaderView];
    [self setUpFooterView];
    [self.tableView reloadData];
}

// 初始化列表数据源
- (void)initTableViewDataSource
{
    self.dataSourceNames = @[@[@"我的定制",@"我的代金券"],@[@"常用地址"],@[@"更多"]];
    self.dataSourceImages = @[@[@"ic_mine_customermade",@"ic_mine_cashcoupon"],@[@"ic_mine_address"],@[@"ic_mine_more"]];
}

-(void)setUpHeaderView{
    self.headerImage.image = [UIImage imageNamed:@"login_banner.jpg"];
    self.tableView.tableHeaderView = self.headerView;
}

- (void)setUpFooterView
{
    // 登录状态
    if (USERINFOManager.getUserInfo.loginStatus) {
        [self loginSuccess];
    }
    // 不登录状态
    else{
        [self logoutSuccess];
    }
}

#pragma mark ---- 列表代理 ----
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataSourceNames.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dataSourceNames[section] count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 12;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyInfomationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyInfomationCell"];
    cell.title.text = self.dataSourceNames[indexPath.section][indexPath.row];
    cell.icon.image = [UIImage imageNamed:self.dataSourceImages[indexPath.section][indexPath.row]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    // 我的定制
    if (indexPath.section == 0 && indexPath.row == 0)
    {
        if (USERINFOManager.getUserInfo.loginStatus == YES) {
            MyCustomViewController *vc = [[UIStoryboard storyboardWithName:@"MyCustom" bundle:nil] instantiateViewControllerWithIdentifier:@"MyCustomViewController"];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            [self jumperLogin];
        }
    }
    // 我的代金券
    else if (indexPath.section == 0 && indexPath.row == 1)
    {
        if (USERINFOManager.getUserInfo.loginStatus == YES) {
            TicketViewController *ticket = [[UIStoryboard storyboardWithName:@"MyTicket" bundle:nil]  instantiateViewControllerWithIdentifier:@"TicketViewController"];
            [self.navigationController pushViewController:ticket animated:YES];
        }else{
            [self jumperLogin];
        }
    }
    // 常用地址
    else if (indexPath.section == 1 && indexPath.row == 0)
    {
        if (USERINFOManager.getUserInfo.loginStatus == YES) {
            CommonAddressViewController *vc = [[UIStoryboard storyboardWithName:@"CommonAddress" bundle:nil] instantiateViewControllerWithIdentifier:@"CommonAddressViewController"];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            [self jumperLogin];
        }
    }
    // 邀请有礼
    else if (indexPath.section == 1 && indexPath.row == 1)
    {
        
        if (USERINFOManager.getUserInfo.loginStatus == YES) {
            H5BaseViewController *h5Base = [[H5BaseViewController alloc ] init];
            h5Base.webViewUrl = InviteGift;
            h5Base.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:h5Base animated:YES];
        }else{
            [self jumperLogin];
        }
    }
    // 投诉建议
    else if (indexPath.section == 2 && indexPath.row == 0)
    {
        SettingViewController *vc = [[UIStoryboard storyboardWithName:@"Setting" bundle:nil] instantiateViewControllerWithIdentifier:@"SettingViewController"];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
        
        
//        if (USERINFOManager.getUserInfo.loginStatus == YES) {
//            FeedBackViewController *vc = [[UIStoryboard storyboardWithName:@"FeedBack" bundle:nil] instantiateViewControllerWithIdentifier:@"FeedBackViewController"];
//            vc.hidesBottomBarWhenPushed = YES;
//            [self.navigationController pushViewController:vc animated:YES];
//        }else{
//            [self jumperLogin];
//        }
    }
    // 更多
    else if (indexPath.section == 2 && indexPath.row == 1)
    {
        SettingViewController *vc = [[UIStoryboard storyboardWithName:@"Setting" bundle:nil] instantiateViewControllerWithIdentifier:@"SettingViewController"];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
}


#pragma mark ---- 登录 ----
- (IBAction)loginAction:(id)sender
{
    [self jumperLogin];
}

#pragma mark ---- 我的订单 ----
- (IBAction)myOrderAction:(id)sender
{
    if (USERINFOManager.getUserInfo.loginStatus == YES) {
        MyOrderViewController *myOrder = [[UIStoryboard storyboardWithName:@"Order" bundle:nil]  instantiateViewControllerWithIdentifier:@"MyOrderViewController"];
        [self.navigationController pushViewController:myOrder animated:YES];
    }else{
        [self jumperLogin];
    }
}

#pragma mark ---- 我的行程 ----
- (IBAction)myJourneyAction:(id)sender
{
    if (USERINFOManager.getUserInfo.loginStatus == YES) {
        TravelViewController *travel = [[UIStoryboard storyboardWithName:@"Travel" bundle:nil]  instantiateViewControllerWithIdentifier:@"TravelViewController"];
        [self.navigationController pushViewController:travel animated:YES];
    }else{
        [self jumperLogin];
    }

}

#pragma mark ---- 通知 ----
-(void)addLoginStatusNotificenter{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginSuccess) name:LoginSuccess object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(logoutSuccess) name:LogoutSuccess object:nil];
}

-(void)removeLoginStatusNotificenter{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(void)loginSuccess{
    self.tableView.tableFooterView = self.footerView;
    self.IDLabel.text = [NSString stringWithFormat:@"ID:%@",USERINFOManager.getUserInfo.MemberId];
}

-(void)logoutSuccess{
    self.tableView.tableFooterView = self.loginFooterView;
    [self.loginButton style1Button];
    self.loginButton.backgroundColor = [UIColor clearColor];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

- (void)dealloc
{
    [self removeLoginStatusNotificenter];
}

@end
